﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using System.Data;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.Announcement.HomeAnnounce
{
    public partial class HomeAnnounceUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }
            if (!IsPostBack)
            {

                DataBind(_userId);
                //using (AnnounceProxy proxy = new AnnounceProxy())
                //{
                //    AnnounceDataSet.T_AnnounceDataTable searchDT = proxy.SearchAnnouncForHome(Int32.Parse(JCYConstant.DisplayCount), _userId);
                    
                //    for(int i=0;i < searchDT.Count;i++)
                //    { 
                //        Label lbAnnEffDate = (Label)this.FindControl("lblAnnEffDate"+i.ToString());
                //        lbAnnEffDate.Text = DateTime.Parse(searchDT.Rows[i]["CREATED_TIME"].ToString()).ToString(JCYConstant.Date_Format_CN);

                //        LinkButton lbAnnSubject = (LinkButton)this.FindControl("lbAnnSubject" + i.ToString());
                //        lbAnnSubject.Text = searchDT.Rows[i]["Subject"].ToString();

                //        Image imgAnnImage = (Image)this.FindControl("imgAnnImage"+i.ToString());

                //        lbAnnSubject.PostBackUrl = string.Format(SPContext.Current.Site.ServerRelativeUrl + "AnnouncePages/ViewAnnounceDetail.aspx?AnnounceId={0}",searchDT.Rows[i]["ID"].ToString());
                       
                //        if (searchDT.Rows[i]["ReadStatus"].ToString() == JCYConstant.ReadStatus.UnRead)
                //        {
                //            imgAnnImage.ImageUrl = "~/_layouts/JM.JCY.Resource/images/Message_unread.png";
                //            imgAnnImage.Width = 24;
                          
                //        }
                //        else
                //        {
                //            imgAnnImage.ImageUrl = "~/_layouts/JM.JCY.Resource/images/Message_Read.png";
                //            imgAnnImage.Width = 17;
                     
                //        }
                //        imgAnnImage.Visible = true;
                //    }
          
                //    for (int i = 5; i > 0; i--)
                //    {
                //        if ((i+1) > searchDT.Count)
                //        {
                //            Image imgAnnImage = (Image)this.FindControl("imgAnnImage" + i.ToString());
                //            imgAnnImage.Visible = false;

                //            LinkButton lbAnnSubject = (LinkButton)this.FindControl("lbAnnSubject" + i.ToString());
                //            lbAnnSubject.Visible = false;
                //        }
                //    }
              
                    //this.dlAnnounce.DataSource = searchDT;
                    //this.dlAnnounce.DataBind();
                //}
            }
        }

        protected void dlAnnounce_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //    // load date format
            Label lblEffDate = (Label)e.Item.FindControl("lblEffDate");
            string dateString = lblEffDate.Text;
            DateTime dateTime = DateTime.Parse(dateString);
            lblEffDate.Text = dateTime.ToString(JCYConstant.Date_Format_CN);

            Image img = (Image)e.Item.FindControl("Image1");
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            string readStatus = lblStatus.Text;


            if (readStatus == JCYConstant.ReadStatus.UnRead)
            {
                img.ImageUrl = "/_layouts/JM.JCY.Resource/images/Message_unread.png";
                img.Width = 24;

            }
            else
            {
                img.ImageUrl = "/_layouts/JM.JCY.Resource/images/Message_Read.png";
                img.Width = 17;

            }

        }

        private void DataBind(int userId)
        {
            using (AnnounceProxy proxy = new AnnounceProxy())
            {
                AnnounceDataSet.T_AnnounceDataTable searchDT = proxy.SearchAnnouncForHome(Int32.Parse(JCYConstant.DisplayCount), userId);
                this.dlAnnounce.DataSource = searchDT;
                this.dlAnnounce.DataBind();
            }
        }

        
    }
}
